
(function () { 
	Lhs.ui.CreateCompanyDetailWindow = function (arg) {
	var pathimage=Ti.App.Properties.getString('pathimage');
	var contact_phone='',company='';
	var html='';
    var win = Titanium.UI.createWindow({
        id: 'CompanyDetail',
        title: 'THÔNG TIN CÔNG TY',
        fullscreen: true,
        backgroundImage:pathimage+'bg.png',
        navBarHidden:true,
    });
    
    var egc = new globals.EventGarbageCollection(win);
    egc.addEventListener('back_'+win.id, function () {
    	Lhs.navGroup.close(win, {
             animated: true
        });
    	
	});
	egc.addEventListener('callTelephone', function () {
		if(contact_phone=='')
		{
			
			require('settings').createAlertDialog(Globals.title,'Chưa cập nhật số điện thoại.');
			
		}
		else
		{
			/*
			var confirm = Titanium.UI.createAlertDialog({ 
					            title: Globals.title, 
					            message:'Bạn có muốn gọi tới số '+ contact_phone, 
					            buttonNames: [Globals.btOk,Globals.btCancel]
					    });
		    confirm.addEventListener('click', function(e) {
		    	  //Clicked cancel, first check is for iphone, second for android
		       if (e.cancel === e.index || e.cancel === true) {
		          return;
		       }
		       switch (e.index) {
		          case 0: {
		          			 Titanium.Platform.openURL('tel:'+contact_phone);
						  }
		          break;
		 
		          //This will never be reached, if you specified cancel for index 1
		          case 1: Titanium.API.info('Clicked button 1 (NO)');
		          break;
		 
		          default:
		          break;
		      }
		 
		    });
			confirm.show();   
			*/
			var message='Gọi tới:'+company+'\nĐiện thoại:'+ contact_phone
			require('settings').createAlertDialog2(Globals.title,message,Call,contact_phone);
			
		}
    	
	});
	
	 var Call = function(e) { 
	 	Titanium.Platform.openURL('tel:'+e.contact_phone);
	 }
	//HEADER ON WINDOWS
    var main_header=require('controls/header').Createheader({
	  	idWinParent:win.id,
	   	height:Ti.App.Properties.getInt('height_Header'),//44,
	  	title:'THÔNG TIN CÔNG TY',
	  	IsCall:1,
    })
	win.add(main_header);
   
	//Background
	var height_header1=63;space=23;left_right=30;height_vi_header_top=41
	if (Ti.Platform.osname == 'iphone')
	{
		height_header1=43;
		space=16;
		left_right=17;
		height_vi_header_top=30
	}
	
	var vi_header1 = Titanium.UI.createView({
		top:main_header.top+main_header.height,
		width:'100%',
		height:height_header1,
		backgroundImage: pathimage+'bg_log_middle_top_title.9.png',
	}); 
	win.add(vi_header1)
	
	var vi_header2 = Titanium.UI.createView({
		top:vi_header1.top+vi_header1.height,
		width:'100%',
		//height:500,
		bottom:0,
		backgroundImage: pathimage+'bg_log_middle.9.png',
	}); 
	win.add(vi_header2)
	
	///WEBVIEW
	var webview = Titanium.UI.createWebView({
	    top:vi_header1.top+vi_header1.height-space,
	    left:left_right,
	    right:left_right,
	    disableBounce:true,
	    touchEnabled:true, //optional
	   // scalesPageToFit:true
	});
	win.add(webview)
	
	
	///Header top
	var vi_header_top = Titanium.UI.createView({
		top:main_header.top+main_header.height,
		width:117,
		height:height_vi_header_top,
		backgroundImage: pathimage+'bg_log_tab0.png',
	}); 
	win.add(vi_header_top)
	
	
	
	
	/*
	'<div class="logocontact">'+
	'	<table cellpadding="0" cellspacing="1" border="0" width="96%" class="contact">'+
	'			<tbody><tr>'+
	'				<td class="title" width="20%">Công ty:</td>'+
	'				<td width="30%">ONEMEDIA</td>'+
	'				<td class="title" width="20%">Người phụ trách:</td>'+
	'				<td width="30%" onemedia=""></td>'+
	'			</tr>'+
	'			<tr>'+
	'			<td class="title">Địa chỉ:</td>'+
	'				<td>Room 1104 - 11 Floor - 185 Giang Vo Str - Ba Dinh Dist - Ha Noi - Viet Nam</td>'+
	'				<td class="title">Website:</td>'+
	'				<td><a href="" target="_blank">www.one-media.vn</a></td>'+
	'			</tr>'+
	'			<tr>'+
	'				<td class="title">Fax:</td>'+
	'				<td>84.4.5121074</td>'+
	'				<td class="title">Địa chỉ email:</td>'+
	'				<td><a href="mailto:one@one-media.vn">one@one-media.vn</a></td>'+
	'			</tr>'+
	'			<tr>'+
	'				<td class="title">Điện thoại:</td>'+
	'				<td>84.4.5121074/78</td>'+
	'			<td class="title">&nbsp;</td>'+
	'				<td>&nbsp;</td>'+
	'			</tr>'+
	'		</tbody></table>'+
	'	</div>';
	*/
	
	
	
	
	
    var LoadListData = function(e) {
    	var datajson=e.datajson;
 		json = JSON.parse(datajson);
 		
		return json;
    }
    var LoadListDataLocal = function(e) {
    	// Ti.API.info ('dang thuc hien ne')
 		var json=require('db').selectCompanyDetail(arg.cid);
		return json;
    }
    
    var LoadList = function(e) { 
    	/*
		var datajson=e.datajson;
		//Ti.API.info (datajson)
 		var tableData=[];
 		//Convert Text to Json Array
 		json = JSON.parse(datajson);
 		Ti.API.info (json)
 		*/
 		
 		
 		var tableData=[];
 		var json=[];
 		
 		if(e.isconnect==1)
 		{
 			json=LoadListData(e);
 		}
 		else
 		{
 			json=LoadListDataLocal(e);
 		}
 		if(json.length==0)
	    {
	    	require('settings').Alert_error(win,Globals.connect_pop);
	    }
 		// Ti.API.info ('DL'+json)
 		//alert(json.length)
	 	if(json.length!=0)
	 	{
	 		var item = json[0];
	 		contact_phone=json[0].contact_phone
	 		contact_phone=contact_phone.replace(' ', '');
	 		contact_phone=contact_phone.replace('(84-8)', '0');
	 		contact_phone=contact_phone.replace('(04)', '04');
	 		contact_phone=contact_phone.replace('+84', '0');
	 		contact_phone=contact_phone.replace('84', '0');
	 		contact_phone=contact_phone.replace('(84-8)', '08');
	 		contact_phone=contact_phone.replace('.', '');
	 		contact_phone=contact_phone.replace('(', '');
	 		contact_phone=contact_phone.replace(')', '');
	 		
	 		company=json[0].title;
	 		//alert(contact_phone)
	 		
	 		if(Ti.Platform.osname == 'android')
			{
				 html=
			 	        '<head><meta content="minimum-scale=1.0, width=device-width, maximum-scale=1.0, user-scalable=yes" name="viewport" />'+
						'</head>'
			}
	 		
	 	    html=html+
				'<h2><center>'+json[0].title+'</center></h2>'+
				'<center><img width="40%"  src="'+json[0].logo+'"></center>'+
				'<p style="text-align:justify">'+json[0].description+'</p>'+
				'<h3>Thông tin liên hệ</h3>'+
				'<p>Địa chỉ:'+json[0].contact_address+'</p>'+
				'<p>Fax:'+json[0].contact_fax+'</p>'+
				'<p>Điện thoại:'+json[0].contact_phone+'</p>'+
				'<p>Người phụ trách:'+json[0].contact_name+'</p>'+
				'<p>Website:'+json[0].contact_web+'</p>'+
				'<p>Địa chỉ email:'+json[0].contact_email+'</p>';
				
			if(json[0].is_showcase==1)
			{
				html=html+
				'<h3>Show sản phẩm</h3>'+
				'<center><img width="100%"  src="'+json[0].file_showcase+'"></center>';
			}
			
		  
		    if(e.isconnect==1)
	 		{
	 			//Insert data when connect
	        	require('db').insertCompanyDetail(item)
	 		}
	 		
	 		  webview.html=html;
		   // win.add(webview);
	 	}
	
 		
 		//Lhs.ui.activityIndicator.hideModal();
	};
	var LoadList_error = function(e) {
		
		
		if(Ti.Platform.osname == 'android')
		{
			require('settings').createAlertDialog(Globals.title,Globals.connecterror);
			//Lhs.ui.activityIndicator.hideModal();
		}
		else
	   		require('settings').Alert_error(win);
	};
	 
	
	
	win.addEventListener('open', function (e) {
    	 if (Ti.Network.online) {
    	 	var jsonurl='http://www.e-media.vn/emedia.php?cid='+arg.cid;
			var param='';
			//Lhs.ui.activityIndicator.showModal(Globals.loading, Globals.timeout, Globals.loadingTimeout);
			require('settings').GetJsonFunction_GET(jsonurl,param,LoadList,LoadList_error);
			
		 }
		 else
		 {
		 	//error in connect with internet
		    //Ti.API.info ('dang thuc hien')
		 	//require('settings').createAlertDialog(Globals.title,Globals.connecterror);
		    var ee=[{isconnect:0}];
		 	LoadList(ee);
		 	//LoadList_error()
		 }
	});
 	return win;
       
    };
})();